//
//  ViewController.m
//  11105
//
//  Created by student on 16/3/6.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic,strong) NSArray *picArr;

@property (nonatomic,weak) UIScrollView *scrollView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.picArr = @[@"down_left",@"down",@"down_right",@"left",@"",@"right",@"up_left",@"up",@"up_right",];
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    UIImage *image = [UIImage imageNamed:@"bg"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    [scrollView addSubview:imageView];
    scrollView.contentSize = image.size;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.bounces = YES;
    self.scrollView = scrollView;
    [self.view addSubview:self.scrollView];
    for (int i = 0; i<3; i++) {
        for (int j = 0; j < 3; j++) {
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(30*j, [UIScreen mainScreen].bounds.size.height - 30*(i+1), 30, 30)];
            [btn setImage:[UIImage imageNamed:self.picArr[3*i+j]] forState:UIControlStateNormal];
            btn.tag = 100 + 3*i+j;
             [btn addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
             [self.view addSubview:btn];
            //保证按钮在最前端的方法1
            [self.view bringSubviewToFront:btn];
        }
    }
    ////保证按钮在最前端的方法2
    //[self.view sendSubviewToBack:scrollView];
}

-(void)pressBtn:(UIButton *)sender{
    int x = self.scrollView.contentOffset.x;
    int y = self.scrollView.contentOffset.y;
    switch (sender.tag) {
        case 100:
        {
            if (x >= 10  && y <= 1000-667) {
                self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x - 10, self.scrollView.contentOffset.y + 10);
            }

        }
            break;
        case 101:
        {
            if(y <= 1000-667){
                self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, self.scrollView.contentOffset.y + 10);
            }
        }
            break;
        case 102:
        {
            if (x <= 1600-375  && y <= 1000-667) {
                self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x + 10, self.scrollView.contentOffset.y + 10);
            }

        }
            break;
        case 103:
        {
            if (x >= 10) {
                self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x - 10, self.scrollView.contentOffset.y);
            }
        }
            break;
        case 105:
        {
            if (x <= 1600-375) {
                self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x + 10, self.scrollView.contentOffset.y);
            }

        }
            break;
        case 106:
        {
            if (x >= 10 && y >= 10) {
                self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x - 10, self.scrollView.contentOffset.y-10);
            }

        }
            break;
        case 107:
        {
            if(y >= 10){
                self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, self.scrollView.contentOffset.y - 10);
            }

        }
            break;
        case 108:
        {
            if (x <= 1600-375 && y >= 10) {
                self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x + 10, self.scrollView.contentOffset.y-10);
            }
        }
            break;
        default:
            break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
